package cn.jnetty.remoting.http;
import cn.jnetty.api.annotation.JComponent;
import cn.jnetty.remoting.AbstractJnettyServer;
import cn.jnetty.remoting.tcp.JnettyTcpSimpleChannelInboundHandlerImpl;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.stream.ChunkedWriteHandler;
/**
 * @author 猪哥亮
 * @email 1030681978@qq.com
 * @date 2020/4/2 16:00
 * 描述：
 */
@JComponent
public class JnettyHttpServer extends AbstractJnettyServer {
    public JnettyHttpServer() {
       super();
    }
    @Override
    public void init(){
        initJnettyConfig();
        port = configuration.getHttpPort();
        bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) {
                ChannelPipeline p = ch.pipeline();
                //websocket协议本身是基于Http协议的，所以需要Http解码器
                p.addLast("http-codec",new HttpServerCodec());
                //以块的方式来写的处理器
                p.addLast("http-chunked",new ChunkedWriteHandler());
                //netty是基于分段请求的，HttpObjectAggregator的作用是将请求分段再聚合,参数是聚合字节的最大长度
                p.addLast("aggregator",new HttpObjectAggregator(getMaxContentLength()));
                p.addLast(new JnettyTcpSimpleChannelInboundHandlerImpl(handler));
            }
        });
    }
}
